package app.fugema.shop.main.center.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Observer;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ActivityGiftOrderDetailBinding;
import app.fugema.shop.main.center.model.GiftOrderBean;
import app.fugema.shop.main.center.model.GiftOrdersDetailResult;
import app.fugema.shop.main.center.vm.JiFenOrderVM;
import app.fugema.shop.main.center.adapter.GiftDetailAdapter;
import app.fugema.shop.main.order.model.OrderAddressHeadBean;
import app.fugema.shop.main.order.model.OrderExpressResult;
import app.fugema.shop.main.order.model.OrderPayFootBean;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DialogUtils;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;

import app.fugema.shop.R;

public class GiftOrderDetailActivity extends BaseTitleActivity<ActivityGiftOrderDetailBinding, JiFenOrderVM>
{
    //待修改 积分订单 详情页

    public static void start(Context context, long orderId)
    {
        Intent intent = new Intent(context, GiftOrderDetailActivity.class);
        intent.putExtra("orderNumber", orderId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gift_order_detail);
    }

    private GiftDetailAdapter giftAdapter;

    private long orderId;

    private String userKey;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initNotificationReceipt()
    {
        super.initNotificationReceipt();
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("积分订单",R.color.white);

        //binding.rvRecycler.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        //viewUtils.setRefreshStateLayout(binding.rvRecycler, this, this);

        giftAdapter = new GiftDetailAdapter();
        binding.rvRecyclerView.setAdapter(giftAdapter);
        //确认收货
        binding.btnConfirm.setOnClickListener(new OnClickCheckedUtil()
        {
            @Override
            public void onClicked(View view)
            {
                DialogUtils.showSimpleDialog(GiftOrderDetailActivity.this,false, (dialog, which) -> {

                    dialog.dismiss();
                    viewModel.confirmGiftOrder(orderId,userKey)
                            .observe(GiftOrderDetailActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                            {
                                @Override
                                protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                {
                                    UIUtils.showToastSafes( data.data.getMsg() );
                                    LiveEventBus.get(AppConstants.EventKey.GIFT_ORDER_RECEIPT, String.class).post(String.valueOf(orderId));
                                    finish();
                                }
                            });

                },"请确保您已收到礼品，再收货！");
            }
        });
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        userKey = LoginUtils.getToken();
        orderId = intent.getLongExtra("orderNumber",0);
        if(orderId == 0 || UIUtils.isEmpty(userKey )) return;
        queryOrderDetail(orderId,userKey);
    }

    private void queryOrderDetail(long mOrderId,String mUserKey)
    {
        viewModel.getOrderDetail(mOrderId,mUserKey)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<GiftOrdersDetailResult>>()
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<GiftOrdersDetailResult> data)
                    {
                        if( data.data.isSuccess() )
                        {
                            //2=待发货，3=待收货，4=已关闭，5=已完成
                            setOrderStatus( data.data.getData().getOrderStatus() );
                            // 物流信息
                            if ( UIUtils.isNotEmpty( data.data.getData().getShipOrderNumber() )  )
                            {
                                queryExpressInfo();
                            }

                            binding.tvOrderStatus.setText( data.data.getData().getShowOrderStatus() );
                            //显示地址
                            OrderAddressHeadBean headBean = OrderAddressHeadBean.getAddressByGiftOrder(data.data.getData());
                            headBean.setState(1);
                            giftAdapter.addHeaderView(headBean, 1);

                            giftAdapter.setDataList( data.data.getData().getItems() );

                            OrderPayFootBean footerBean = OrderPayFootBean.getOrderFootByGiftOrder(data.data.getData());
                            footerBean.setItemType(BaseItemTypeInterface.TYPE_FOOT);
                            giftAdapter.addFooterView(footerBean);
                        }

                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<GiftOrdersDetailResult> data)
                    {
                        return UIUtils.isEmpty(data.data.getData()) ;
                    }
                 });
    }

    private void setOrderStatus(int status)
    {
        //2=待发货，3=待收货，4=已关闭，5=已完成
        if( status == 5 )
        {
            binding.ivStatusImg.setImageResource( R.mipmap.ic_order_complate );
        }
        else if( status == 4 )
        {
            binding.ivStatusImg.setImageResource( R.mipmap.ic_order_refuse );
        }
        else if(status == 3 )
        {
            binding.ivStatusImg.setImageResource( R.mipmap.ic_order_waiting );
            binding.btnConfirm.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.ivStatusImg.setImageResource( R.mipmap.ic_order_waiting );
        }
    }

    private void queryExpressInfo()
    {
        //查物流
        viewModel.checkOrderExpressInfo(orderId ,userKey)
                .observe(this, new BaseViewObserver<LiveDataWrapper<OrderExpressResult>>() {
                    @Override
                    protected void onSuccess(LiveDataWrapper<OrderExpressResult> data)
                    {

                        OrderExpressResult expressBean = data.data;
                        expressBean.setItemType(BaseItemMultiType.TYPE_HEAD_ONE);
                        expressBean.setOrderId(String.valueOf(orderId));
                        giftAdapter.addHeaderView(expressBean, 0);
                        giftAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<OrderExpressResult> data)
                    {
                        return UIUtils.isEmpty(data.data.getComment());
                    }
                });
    }

}
